<template>
  <!-- 对话框(添加 / 修改) -->
  <el-dialog :title="title" :visible.sync="show" width="500px" append-to-body :before-close="cancel">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">

      <college-selector href="college_selector" v-model="form.collegeId" property="collegeId"></college-selector>
      <el-form-item label="课程名称" prop="coursesName">
        <el-input v-model="form.coursesName" placeholder="请输入课程名称"/>
      </el-form-item>
      <el-form-item label="限制人数" prop="limitStudentCount">
        <el-input v-model="form.limitStudentCount" placeholder="请输入限制人数"/>
      </el-form-item>
      <el-form-item label="学年" prop="schoolYear">
        <el-select v-model="form.schoolYear" placeholder="请选择学年">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SCHOOL_YEAR)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="教室ID" prop="classRoomId">
        <el-input v-model="form.classRoomId" placeholder="请输入教室ID"/>
      </el-form-item>
      <el-form-item label="时间范围" prop="timeFrame">
        <el-input v-model="form.timeFrame" placeholder="请输入时间范围"/>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
                    :key="dict.value" :label="parseInt(dict.value)">{{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {createAdditionalCourses, updateAdditionalCourses} from "@/api/study/additionalCourses";
import CollegeSelector from "@/components/CollegeSelector";

export default {
  name: "AdditionalCoursesEdit",
  components: {
    CollegeSelector
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        collegeId: [{required: true, message: "学院ID不能为空", trigger: "blur"}],
        coursesName: [{required: true, message: "课程名称不能为空", trigger: "blur"}],
        limitStudentCount: [{required: true, message: "限制人数不能为空", trigger: "blur"}],
        schoolYear: [{required: true, message: "学年不能为空", trigger: "change"}],
        classRoomId: [{required: true, message: "教室ID不能为空", trigger: "blur"}],
        timeFrame: [{required: true, message: "时间范围不能为空", trigger: "blur"}],
        remark: [{required: true, message: "备注不能为空", trigger: "blur"}],
      }
    };
  },
  created() {
  },
  props: {
    // 是否显示弹出层
    show: {
      type: Boolean,
      default: false
    },
    // 显示弹出层标题
    title: {
      type: String,
      default: ''
    }
  },
  methods: {
    /** 初始化编辑的数据**/
    initData(rowData) {
      this.reset();
      if (rowData && rowData.id) {
        this.form = rowData;
        this.form.datetimerange = [this.form.startTime, this.form.endTime]
      }
    },
    /** 取消按钮 */
    cancel() {
      this.$emit('update:show', false)
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        collegeId: '',
        coursesName: undefined,
        limitStudentCount: undefined,
        schoolYear: undefined,
        classRoomId: undefined,
        timeFrame: undefined,
        remark: undefined,
        status: undefined,
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateAdditionalCourses(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.cancel();
            this.$emit('success')
          });
          return;
        }
        // 添加的提交
        createAdditionalCourses(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.cancel();
          this.$emit('success')
        });
      });
    },

  }
};
</script>
